<?php

function get_min_match_id(){
	$match_id = sql_get("SELECT MIN(ID) AS min_id FROM `Matches` WHERE Status=1 ");
	return $match_id[0]['min_id'];
	}


function get_max_match_id(){
	$match_id = sql_get("SELECT MAX(ID) AS max_id FROM `Matches`");
	$max_id = $match_id[0]['max_id'];
	return $max_id;
	}

function create_match(){
    mysql_query("INSERT INTO Matches( Players_cnt, Turn, Status ) VALUES (' ', ' ', ' ')  ") or die(mysql_error()); 
	return get_max_match_id();	
	}
	

function get_matches($status=0){
	$query = "SELECT `ID`FROM `Matches`WHERE STATUS=$status ";
    $result = sql_get($query);
	//while($match = mysql_fetch_assoc( $result ))
	foreach ($result as $match)
	{
	Print "<input type='radio' name='match_id' value='${match['ID']}' /> ".$match['ID']." <br/>";
	}
	return $result ;
	
}


function get_players(){
	$query = "SELECT `ID` , `Username` FROM `Players`";
	$players = sql_get($query);
	foreach( $players as $player)
	{
Print "<input type='checkbox' name='players[]' value='${player['ID']}' /> ".$player['ID']." ".$player['Username']." <br />";
} 
	
}

function zaideju_sarasas ($match_id){
	
	$query = "SELECT ID, Username FROM Participants, Players WHERE Players.ID = Player_id AND match_id={$match_id} ORDER BY Player_id";
	return sql_get($query);
}

function zaideju_sarasas_html ($match_id, $current_participant_nr){
	
	$zaidejai = zaideju_sarasas ($match_id);
	foreach ($zaidejai as $nr => $z){
		if ($current_participant_nr == $nr){
			$highlight = "style = 'color : red'";
			
		}
		else $highlight = "";
	echo "<li $highlight>".$z["Username"]." ".$nr."</li>";
	}
}

?>
